package guo.xungenguser.service.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guo.xungengcommon.utils.R;
import guo.xungengcommon.vo.TokenGetUser;
import guo.xungengcommon.vo.UserVo;
import guo.xungenguser.dao.UserDao;
import guo.xungenguser.entity.User;
import guo.xungenguser.interceptor.UserInterceptor;
import guo.xungenguser.service.UserService;
import guo.xungenguser.vo.UpdateInfoVo;
import guo.xungenguser.vo.WeiboUser;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class UserserviceImpl extends ServiceImpl<UserDao,User> implements UserService{

    @Autowired
    UserDao userDao;

    @Override
    public R saveUser(TokenGetUser tokenGetUser) {

        UserVo userVo = new UserVo();
        User user = userDao.selectOne(new QueryWrapper<User>().eq("uid", tokenGetUser.getUid()));
//          未登录
          if (user == null){
              User user2 = new User();
              WeiboUser weiboUser = getUserInfoByToken(tokenGetUser);
                user2.setUserName(weiboUser.getName());
                user2.setImg(weiboUser.getUrl());
                user2.setUid(weiboUser.getId());
                user2.setAccessToken(tokenGetUser.getAccessToken());
                userDao.saveWeiboUser(user2);
              BeanUtils.copyProperties(user2,userVo);
//                userDao.saveinfo(null,user2.getUserName(),user2.getRealName(),user2.getPassword(),user2.getSex(),user2.getImg(),user2.getAuthority(),user2.getEmail(),user2.getTel(),user2.getIsAssigned(),user2.getIsDeleted(),user2.getUid(),user2.getAccessToken());
                return R.ok().put("user",userVo);
//          已登录
          }else {
              User user1 = new User();
              BeanUtils.copyProperties(user,user1);
              user1.setAccessToken(tokenGetUser.getAccessToken());
              BeanUtils.copyProperties(user1,userVo);
              userDao.updateById(user1);
//              userDao.updateUserById(user1);
              return R.ok().put("user",userVo);
          }

    }

    private WeiboUser getUserInfoByToken(TokenGetUser tokenGetUser) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("https://api.weibo.com/2/users/show.json?access_token="+tokenGetUser.getAccessToken()+"&uid="+tokenGetUser.getUid())
                .build();
        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            WeiboUser weiboUser = JSON.parseObject(string, new TypeReference<WeiboUser>(){});
            return weiboUser;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *   gitee用户登录测试
     * @param userVo
     * @return
     */
    @Override
    public R saveGiteeUser(UserVo userVo) {

        User user = userDao.selectOne(new QueryWrapper<User>().eq("gitee_id", userVo.getGiteeId()));
        if (user == null){
            User user1 = new User();
            BeanUtils.copyProperties(userVo,user1);
            userDao.saveWeiboUser(user1);
        }else {
            User user1 = new User();
            BeanUtils.copyProperties(userVo,user1);
            userDao.updateById(user1);
        }
        return R.ok();
    }

    @Override
    public void updateImg(String ossUrl) {
        UserVo userVo = UserInterceptor.threadLocal.get();
        Integer id = userVo.getId();
        userDao.updateOssurlById(ossUrl,id);
    }

    @Override
    public R updateAllInfo(UpdateInfoVo updateInfoVo) {
        UserVo userVo = UserInterceptor.threadLocal.get();
        User user = new User();
        BeanUtils.copyProperties(userVo,user);
        BeanUtils.copyProperties(updateInfoVo,user);
        this.updateById(user);
        BeanUtils.copyProperties(user,userVo);
        return R.ok().put("user",userVo);
    }
}
